package cn.tedu.luban.settle.main.service;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Component
public class RocketMQMessageService implements MessageService {

    /**
     * 异步消息发送，记录失败日志，由人工进行处理
     */

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void sendSettledMessage(String no) {

        //1.组织消息
        Message<String> message = MessageBuilder.withPayload(no).build();
        //2.异步消息  onSuccess表示消息发送成功时执行的方法，onException表示消息发送失败时执行的方法
        rocketMQTemplate.asyncSend("order_complete_topic", message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("发送成功");
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("发送失败");
            }
        });

    }
}
